<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>数据字典明细</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="12"
          ><div class="grid-content bg-purple">
            <el-input placeholder="请输入内容" clearable>
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="addDictInfoDialogVisible = true"
              >新增明细</el-button
            >
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="success">刷新</el-button>
          </div>
        </el-col>
      </el-row>

      <!-- 数据字典列表区域 -->
      <el-table border stripe width="100%" :data="dataDictionaryInfoList">
        <el-table-column fixed label="序号" width="100" prop="id">
        </el-table-column>
        <el-table-column fixed label="类型" width="150" prop="type">
        </el-table-column>
        <el-table-column fixed label="code" width="100" prop="code">
        </el-table-column>
        <el-table-column fixed label="字典编号" width="200" prop="dictId">
        </el-table-column>
        <el-table-column fixed label="明细信息" width="100" prop="info">
        </el-table-column>
        <el-table-column fixed label="操作">
          <template slot-scope="scope">
            <!-- 第一个是查看详情按钮 -->
            <el-tooltip
              effect="dark"
              content="查看"
              placement="top"
              :enterable="false"
            >
              <el-button
                v-auth="['ADMIN']"
                type="success"
                icon="el-icon-check"
                circle
                @click="showDialog(scope.row.id)"
              ></el-button>
            </el-tooltip>

            <!-- 第二个删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button
                v-auth="['ADMIN']"
                type="danger"
                icon="el-icon-check"
                circle
                @click="removeDialog(scope.row.id)"
              ></el-button>
            </el-tooltip>

            <!-- 第三个修改按钮 -->
            <el-tooltip
              effect="dark"
              content="修改"
              placement="top"
              :enterable="false"
            >
              <el-button
                v-auth="['ADMIN']"
                type="primary"
                icon="el-icon-check"
                circle
                @click="updateDialog(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current"
        :page-sizes="[5, 10]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 查看数据字典详情对话框 -->
    <el-dialog
      title=" 查看数据字典明细"
      :visible.sync="showDictInfoDialogVisible"
      width="50%"
      center
    >
      <!-- 内容主体区 -->
      <el-form :model="showForm" label-width="100px">
        <el-form-item label="id">
          <el-input v-model="showForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-input v-model="showForm.type" disabled></el-input>
        </el-form-item>
        <el-form-item label="code">
          <el-input v-model="showForm.code" disabled></el-input>
        </el-form-item>
        <el-form-item label="明细信息">
          <el-input v-model="showForm.info" disabled></el-input>
        </el-form-item>
        <el-form-item label="数据字典编号">
          <el-input v-model="showForm.dictId" disabled></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDictInfoDialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

    <!-- 删除字典明细信息对话框 -->
    <el-dialog
      title="删除字典明细信息"
      :visible.sync="removeDictInfoDialogVisible"
      width="30%"
      center
    >
      <span>确定删除掉:{{ removeDictInfoForm.info }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="removeDictInfoDialogVisible = false"
          >关 闭</el-button
        >
        <el-button type="primary" @click="removeDictInfo">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 更新数据字典详情对话框 -->
    <el-dialog
      title=" 查看数据字典明细"
      :visible.sync="updateDictInfoDialogVisible"
      width="50%"
      center
    >
      <!-- 内容主体区 -->
      <el-form :model="showForm" label-width="100px">
        <el-form-item label="id">
          <el-input v-model="showForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-input v-model="showForm.type" disabled></el-input>
        </el-form-item>
        <el-form-item label="code">
          <el-input v-model="showForm.code" disabled></el-input>
        </el-form-item>
        <el-form-item label="明细信息">
          <el-input v-model="showForm.info"></el-input>
        </el-form-item>
        <el-form-item label="数据字典编号">
          <el-input v-model="showForm.dictId" disabled></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDictInfoDialogVisible = false"
          >关 闭</el-button
        >
        <el-button type="primary" @click="updateDictInfo">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加明细对话框 -->
    <el-dialog
      title="添加数据字典明细"
      :visible.sync="addDictInfoDialogVisible"
      width="50%"
      @close="addDictInfoDialogClosed"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addDictInfoForm"
        :rules="addDictInfoRules"
        ref="addDictInfoRef"
        label-width="100px"
      >
        <el-form-item label="code" prop="code">
          <el-input v-model="addDictInfoForm.code"></el-input>
        </el-form-item>

        <el-form-item label="字典编号" prop="dictId">
          <el-input v-model="addDictInfoForm.dictId"></el-input>
        </el-form-item>

        <el-form-item label="内容" prop="info">
          <el-input v-model="addDictInfoForm.info"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDictInfoDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSavaDictInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  pageDictInfo,
  removeDictInfo,
  getDictInfo,
  saveDictInfo,
  updateDictInfo,
} from "@/api/datadictionary.js";
export default {
  data() {
    return {
      //获取用户列表的参数对象
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      total: 0,

      dataDictionaryInfoList: [],
      //详情的显示与关闭
      showDictInfoDialogVisible: false,
      showForm: {},
      //删除弹窗的显示与关闭
      removeDictInfoDialogVisible: false,
      //保存删除的详情信息对象
      removeDictInfoForm: {},
      //修改弹窗的显示与关闭
      updateDictInfoDialogVisible: false,
      //新增明细弹窗的显示与关闭
      addDictInfoDialogVisible: false,

      //增加用户的表单
      addDictInfoForm: {
        code: "",
        dictId: "",
        info: "",
      },
      //增加用户表单的规则
      addDictInfoRules: {
        code: [
          {
            required: true,
            message: "请输入code",
            trigger: "blur",
          },
        ],
        dictId: [
          {
            required: true,
            message: "请输入字典编号",
            trigger: "blur",
          },
        ],
        info: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getDataDictionaryInfoList();
  },
  methods: {
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getDataDictionaryInfoList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getDataDictionaryInfoList();
    },

    async getDataDictionaryInfoList() {
      const { data: res } = await pageDictInfo(
        this.queryInfo.current,
        this.queryInfo.size
      );
      this.dataDictionaryInfoList = res.data.records;

      this.total = res.data.total;
    },

    //展示数据字典详情的对话框
    showDialog(id) {
      getDictInfo(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.showDictInfoDialogVisible = true;
          this.showForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //删除详情提示框展示
    removeDialog(id) {
      getDictInfo(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeDictInfoDialogVisible = true;
          this.removeDictInfoForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    // 点击确定删除
    removeDictInfo() {
      var id = this.removeDictInfoForm.id;

      removeDictInfo(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeDictInfoDialogVisible = false;
          this.getDataDictionaryInfoList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //修改详情提示框展示
    updateDialog(id) {
      getDictInfo(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.updateDictInfoDialogVisible = true;
          this.showForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //  确认修改
    updateDictInfo() {
      var EditDictInfo = {
        id: this.showForm.id,
        type: this.showForm.type,
        code: this.showForm.code,
        info: this.showForm.info,
        dictId: this.showForm.dictId,
      };

      updateDictInfo(EditDictInfo.id, EditDictInfo.info)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.updateDictInfoDialogVisible = false;
          this.getDataDictionaryInfoList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //重置新增明细填写的窗口
    addDictInfoDialogClosed() {
      this.$refs.addDictInfoRef.resetFields();
    },
    onSavaDictInfo() {
      var DictInfo = {
        code: this.addDictInfoForm.code,
        dictId: this.addDictInfoForm.dictId,
        info: this.addDictInfoForm.info,
      };
      saveDictInfo(DictInfo)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.addDictInfoDialogVisible = false;
          this.getDataDictionaryInfoList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>